package com.atwisdom.star.common.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtils {

    public static LocalDate ConvertDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    public static LocalDate ConvertDateStrToLocalDate(String dateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(dateStr, formatter);
    }


    public static Date ConvertLocalDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }

    public static String getLocalDateFormat(LocalDate localDate, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(dateTimeFormatter);
    }

    public static String getLocalDateFormat(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateFormat(Date date, String pattern) {
        if(date==null){
            return "";
        }
        LocalDate localDate = DateUtils.ConvertDateToLocalDate(date);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(dateTimeFormatter);
    }

    public static String getLocalDateTimeFormat(Date date, String pattern) {
        if(date==null){
            return "";
        }
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static LocalDateTime getLocalDateTime(LocalDate date,String timeStr) {
        String[] timeStrAry = timeStr.split(":");
        if(timeStrAry[0].length() == 1){
            timeStr = "0"+timeStr;
        }
        LocalTime time = LocalTime.parse(timeStr); // 解析时间字符串
        return LocalDateTime.of(date, time);
    }

    /**
     * 获取日期加上一个小时数（double）的时间
     * @param dateTime
     * @param hours
     * @return
     */
    public static LocalDateTime getLocalDateTimePlusHours(LocalDateTime dateTime,Double hours) {
        int wholeHours = (int)Math.floor(hours);  // 将小时数转换为整数部分
        int minutes = (int) ((hours - wholeHours) * 60);  // 将小数部分转换为分钟数
        LocalDateTime resultDateTime = dateTime.plusHours(wholeHours).plusMinutes(minutes);
        return resultDateTime;
    }

    /**
     * 获取日期减去一个小时数（double）的时间
     * @param dateTime 时间 年月日时分秒
     * @param hours 小时
     * @return 时间
     */
    public static LocalDateTime getLocalDateTimeMinusHours(LocalDateTime dateTime,Double hours) {
        int wholeHours = (int)Math.floor(hours);  // 将小时数转换为整数部分
        int minutes = (int) Math.ceil((hours - wholeHours) * 60);  // 将小数部分转换为分钟数
        LocalDateTime resultDateTime = dateTime.plusHours(-wholeHours).plusMinutes(-minutes);
        return resultDateTime;
    }

    /**
     * 获取当月第一天
     * @param year
     * @param month
     * @return
     */
    public static String getStartOfMonth(int year, int month) {
        // 计算当月的第一天 yyyy-MM-dd
        DateTime parseDateTime = DateUtil.parse(year + "-" + month + "-01", "yyyy-MM-dd");
        String firstDayOfMonth = DateUtil.beginOfMonth(parseDateTime).toString();
        System.out.println("当月的第一天：" + firstDayOfMonth);
        return firstDayOfMonth;
    }

    /**
     * 获取当月最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getEndOfMonth(int year, int month) {
        // 计算当月的最后一天 yyyy-MM-dd
        DateTime parseDateTime = DateUtil.parse(year + "-" + month + "-01", "yyyy-MM-dd");
        String lastDayOfMonth = DateUtil.endOfMonth(parseDateTime).toString();
        System.out.println("当月的最后一天：" + lastDayOfMonth);
        return lastDayOfMonth;
    }




    /**
     * 获取两个时间之前小时数
     *
     * @param start 开如时间
     * @param end   结束时间
     * @return 小时数，小数部份为分秒
     */
    public static Double getHoursBetween(Date start, Date end) {
        Instant startInstant = start.toInstant();
        LocalDateTime startLocalDateTime = startInstant.atZone(ZoneId.systemDefault()).toLocalDateTime();

        Instant endInstant = start.toInstant();
        LocalDateTime endLocalDateTime = endInstant.atZone(ZoneId.systemDefault()).toLocalDateTime();

        long secondsBetween = ChronoUnit.SECONDS.between(startLocalDateTime, endLocalDateTime);
        double hoursBetween = secondsBetween / 3600.0;
        return hoursBetween;
    }
}
